package com.pipayshop.service.impl;

import com.pipayshop.domain.StoreEvaluate;
import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.domain.vo.StoreEvaluateVO;
import com.pipayshop.mapper.PhysicalStoreEvaluateMapper;
import com.pipayshop.service.PhysicalStoreEvaluateService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.utils.StringUtil;
import com.pipayshop.utils.ValidationUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 网店评价 服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-07-29
 */
@Service
public class PhysicalStoreEvaluateServiceImpl extends ServiceImpl<PhysicalStoreEvaluateMapper, StoreEvaluate> implements PhysicalStoreEvaluateService {

    @Resource
    private PhysicalStoreEvaluateMapper shopEvaluateMapper;

    /**
     * 根据实体店id获取实体店评价列表
     */
    @Override
    public PageDataVO getShopEvaluateListByItemId(Integer page, Integer limit, String shopId) {
        List<StoreEvaluateVO> list = shopEvaluateMapper.selectPageByShopId((page - 1) * limit, limit, shopId);
        int count = shopEvaluateMapper.selectPageByShopIdCount(shopId);
        return new PageDataVO(count, list);
    }


    /**
     * 增加实体店评价
     */
    @Override
    public boolean addShopEvaluate(StoreEvaluate shopEvaluate) {
        // 校验必填字段
        ValidationUtil.validateString(shopEvaluate.getShopId(), "shop_id_required");
        ValidationUtil.validateString(shopEvaluate.getUserId(), "user_id_required");
        ValidationUtil.validateString(shopEvaluate.getCommodityId(), "commodity_id_required");
        ValidationUtil.validateString(shopEvaluate.getEvaluate(), "review_content_required");
        ValidationUtil.validateScore(shopEvaluate.getScore(),1,5,"comment_score_out_of_range");
        // 设置评价ID
        shopEvaluate.setEvaluateId(StringUtil.generateShortId());
        // 执行插入操作
        int result = shopEvaluateMapper.insert(shopEvaluate);
        return result > 0;
    }


    /**
     * 删除实体店评价列表
     */
    @Override
    public boolean deleteShopEvaluate(String evaluateId, String userId) {
        int result = shopEvaluateMapper.update(null, new UpdateWrapper<StoreEvaluate>()
                .eq("evaluate_id", evaluateId)
                .eq("user_id", userId)
                .set("status", 1));
        return result>0;
    }
}
